<?php
class ControllerCatalogSpecials extends AdminController {
	
	public function index() {
		M('catalog/specials');
		$this->getList();
	}

	public function update() {
		M('catalog/specials');
		if ($this->request->isPost()) {
			$this->model_catalog_specials->editSpecial($this->request->get['product_special_id'], $this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/specials'));
		}
		$this->getForm();
	}

	public function delete() {
		M('catalog/specials');

		if (isset($this->request->post['selected'])) {
			$ids = $this->request->post['selected'];
			$ids = implode(',', $ids);
			$this->model_catalog_specials->deleteSpecials($ids);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('catalog/specials'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		$this->data['cur_time'] = time();

		$qf = new Query_filter();
		
		$specials_total = $this->model_catalog_specials->getTotalSpecials();
		$this->data['specials'] = $this->model_catalog_specials->getSpecials($qf);
 
		$this->data['pagination'] = $this->pagination($specials_total)->render();

		$this->display('catalog/special_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		$this->data['action'] = UA('catalog/specials/update', 'product_special_id=' . $this->request->get['product_special_id']);

		$this->data['special'] = $this->model_catalog_specials->getSpecial($this->request->get['product_special_id']);

		M('sale/customer_group');
		$this->data['customer_group'] = $this->model_sale_customer_group->getCustomerGroups();

		$this->display('catalog/special_form.tpl');
	}
}
?>